package com.zwb.aiwebsite.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 封装的返回结果
 *
 * @author 2825075112@qq.com
 */
@Data
@ApiModel("封装的响应结果")
public class ResponseResult<T> {

    /**
     * 响应信息
     */
    @ApiModelProperty("响应信息")
    private String msg;

    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private int code;

    /**
     * 数据
     */
    @ApiModelProperty("数据")
    private T data;

    public ResponseResult(ResultCode resultCode) {
        this.msg = resultCode.getMsg();
        this.code = resultCode.getCode();
    }

    public ResponseResult(String msg, int code) {
        this.msg = msg;
        this.code = code;
    }

    /**
     * 成功
     */
    public static <T> ResponseResult<T> ok() {
        return new ResponseResult<T>(ResultCode.OK);
    }

    /**
     * 带数据的成功
     */
    public static <T> ResponseResult<T> okOf(T data) {
        return new ResponseResult<T>(ResultCode.OK).data(data);
    }

    /**
     * 失败
     */
    public static <T> ResponseResult<T> fail() {
        return new ResponseResult<T>(ResultCode.FAIL);
    }

    /**
     * 错误
     */
    public static <T> ResponseResult<T> error() {
        return new ResponseResult<T>(ResultCode.SERVER_ERROR);
    }

    /**
     * 带错误信息的错误
     */
    public static <T> ResponseResult<T> error(String msg) {
        return new ResponseResult<T>(ResultCode.SERVER_ERROR).msg(msg);
    }




    /**
     * 构建数据
     */
    public ResponseResult<T> data(T data) {
        this.data = data;
        return this;
    }

    /**
     * 构建数据
     */
    public ResponseResult<T> msg(String msg) {
        this.msg = msg;
        return this;
    }
}
